package com.CST.util;

import com.CST.common.api.*;
import java.util.*;
import org.springframework.util.*;

/**
 * @Description:分页工具
 * @Author: wzw
 * @Time: 2020/3/9 16:09
 */
public class PageUtil<E> {


    /**
     * 分页
     *
     * @param list     数据
     * @param pageNum  页数
     * @param pageSize 条数
     * @return
     */
    public ResultPageObject listPagination(List<E> list, Integer pageNum, Integer pageSize) {
	  ResultPageObject resultPageObject = new ResultPageObject();
	  if (CollectionUtils.isEmpty(list)) {
		resultPageObject.setCode(200);
		resultPageObject.setErrMsg("无数据");
		return resultPageObject;
	  }
	  final int total = list.size();
	  int totalSize = pageNum * pageSize;
	  if (totalSize > total) {
		totalSize = total;
	  }
	  int start = (pageNum - 1) * pageSize;
	  List<E> newArray = new ArrayList<>(pageSize);
	  for (int i = start; i < totalSize; i++) {
		newArray.add(list.get(i));
	  }

	  resultPageObject.setCode(200);
	  resultPageObject.setPageCurrent(pageNum);
	  resultPageObject.setPageSize(pageSize);
	  resultPageObject.setTotal(Long.parseLong(String.valueOf(total)));
	  int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
	  resultPageObject.setTotalPages(Long.parseLong(String.valueOf(totalPage)));
	  resultPageObject.setData(newArray);
	  return resultPageObject;
    }
}
